<?php
session_start();
require_once 'includes/class.phpmailer.php';

	require_once "includes/config.php";
	require_once "includes/pagination.php";
    $itemPerPage=10;	
	$sg = $_GET['sg'];
	$key = $_GET['key'];
	$state = $_GET['st'];
	$city = $_GET['cty'];
	$priceFrom = $_GET['pf'];
	$priceTo = $_GET['pt'];
	
	if(!empty($_GET['sg'])) {	
	
	 if(is_numeric($state))
		 $state = getStateName($state);
	 if(is_numeric($city))
		$city = getCityName($city);
		
				
	 if($city!="" || $state!="")
		  if($city!="" && $state!="") 
			$searchFor=$state." - ".$city;
		  else if($state!="")
		   	$searchFor=$state;
		  else if($city!="")
		   		$searchFor=$city;
				
	 if($_GET['sg']=="merchant")
	   {
	   if($key!='' || $state!='' || $city!='' || $priceFrom!='' || $priceTo!='')
	     {
		   $sqlSelWish="SELECT *
					   FROM tbl_whole_wishlist, tbl_users
					   WHERE tbl_users.user_type=3 and tbl_whole_wishlist.user_id = tbl_users.user_id";
					   
		   if($key!='')
		    $sqlSelWish.=" AND tbl_whole_wishlist.product_name LIKE '%$key%' "; 
		   if( $state!='')
	        $sqlSelWish.=" AND tbl_users.state LIKE '%$state%' "; 
		   if( $city!='')
	        $sqlSelWish.=" AND tbl_users.city LIKE '%$city%' "; 
		   if($priceFrom!='' && is_numeric($priceFrom))
		    $sqlSelWish.=" AND tbl_whole_wishlist.price>='$priceFrom' "; 
		   if($priceTo!='' && is_numeric($priceTo))
		    $sqlSelWish.=" AND tbl_whole_wishlist.price<='$priceTo' "; 
	     }
		 else
		    $sqlSelWish="SELECT * FROM tbl_whole_wishlist WHERE 0";		
       }
	else {
		$sqlSelWish="SELECT * FROM tbl_whole_wishlist WHERE 0";
		$pagelink="srchBuyer&sg";
	}
	$pLink = "srchBuyer&sg=$sg&key=$key&st=$state&cty=$city&pf=$priceFrom&pt=$priceTo";
	
	
	$pos = strripos($sqlSelWish, "where");
	if ($pos === false) {
		//echo "Not found";
	} else {
		//echo "found";
	}
	if(!empty($_POST['hidBargain'])) {
$offerAmount = $_POST['txtOffer'];
$comment = $_POST['txtComment'];

$emailContent="You are receiving this message because (".$_SESSION['user_name'].") has made an offer towards (<a href=index.php?wholeproducts=".$_POST['prodId'].">".$_POST['pname']."</a>)<br><p>Offer: $".number_format($offerAmount,2)."<p><p>".$comment."</p>";
				
								
mysql_query("insert into tbl_messages(senderid,recieverid,productid,subject,message,posteddate,status,type,itemid,section) values('".$_SESSION['user_id_sess']."','".$_POST['recieverid']."','".$_POST['prodId']."','Offer Recieved','".addslashes($emailContent)."','".date("Y-m-d")."','1','Bargain','".$itemId."','".$_POST['section']."')");

 $mailmsg="<table width='100%' border='0' style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px; line-height:25px'>
 
  <tr>
    <td height='32' colspan='2' style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:11px;'>".$emailContent."</td>
  </tr>
  
 
</table>
";
		$mail = new PHPMailer(); // defaults to using php "mail()"
	  	$mail->MsgHTML($mailmsg);
		$mail->From       = "mail@swapen.com";
		$mail->FromName   = "Swapen.com";
		$mail->Subject    = "Offer Recieved";
		//$mail->AddAddress("suggestions@swapen.com", $firstName);
		$mail->Send();
									
									echo "<div id='suc-msg'>Offer placed successfully.</div>"; 
}	
?>
<script language="javascript1.2">
function sendOffer(frmid)
{
frmid.submit();
}
</script>	

<div id='panel'>
<table width="100%" border="0">
  <tr>
    <td class="contentHeader"><h2>Search Results <?php if(!empty($searchFor)) echo " - ".$searchFor; ?></h2></td>
  </tr>
  <tr>
    <td>

		 <?php 		   
				   if(!empty($_GET['sortby'])) {
				   		if($_GET['sortby']=='lps') {
							$sqlSelWish .= " order by tbl_whole_wishlist.price ASC";		
						}
						else if($_GET['sortby']=='hps') {
							$sqlSelWish .= " order by tbl_whole_wishlist.price DESC";		
						}
						else if($_GET['sortby']=='bm') {
							$sqlSelWish .= " order by tbl_whole_wishlist.wishlist_id";		
						}
						else if($_GET['sortby']=='mst') {
							$sqlSelWish .= " order by tbl_whole_wishlist.created_date ASC";		
						}
				   }
				   
				   $rstSelWish=mysql_query(getPagingQuery($sqlSelWish,$itemPerPage));			  
				   $num=mysql_num_rows($rstSelWish);				   
				   if($num==0)
					{					
					  	echo "No Products Found...";
					 
					}
				   else { ?>
					   <br><table width='670' border='0' align='center' cellspacing='0'>
						       <tr>
                                <td width="307" style="text-align:right">Sort by:</td>
						   	  <td width="359">
						  <table align="center" width="100%" border="0">
										<tr>
											<td width="22%"><a href="index.php?<?php echo $pLink; ?>&sortby=bm">Best Match</a></td>
											<td width="25%"><a href="index.php?<?php echo $pLink; ?>&sortby=lps">Most Recent</a></td>
										  <td width="27%"><a href="index.php?<?php echo $pLink; ?>&sortby=lps">Price Low-Hi</a></td>
										  <td width="26%"><a href="index.php?<?php echo $pLink; ?>&sortby=hps">Price Hi-Low</a></td>
							</tr>
								 </table></td>
                                   
					     </tr>
                       <?php
						 while($rowSelWish=mysql_fetch_array($rstSelWish))
					     {							   
						   $itemId=$rowSelWish['wishlist_id'];
						   $productName=wordFormater($rowSelWish['product_name'],30);
						   $productTitle = $rowSelWish['product_name'];					   
						   $productPrice=$rowSelWish['price'];
						   $wishListType=$rowSelWish['wishlist_type'];
						   $userName=$rowSelWish['username'];
						   $userCity=$rowSelWish['city'];
						   $postedDate=$rowSelWish['posted_date'];
						   
						   if($wishListType==1) { 
						     $itemId=$rowSelWish['item_id'];
							 $pimage = getWholeProductImage($itemId,'p');
							 if($pimage['thumb']!="") {
								$productThumb = $pimage['thumb'];
							 }  else {
								$productThumb="images/no_image.gif";	
							 } 
						   }
						   else if(!empty($rowSelWish['product_thumb']))
						    $productThumb=$rowSelWish['product_thumb'];
						   else
						     $productThumb="images/no_image.gif"; 
						   if($wishListType=='2')
							$link='index.php?wwishproduct='.$itemId;
						   else
						    $link='index.php?wholeproducts='.$itemId;
						   ?>	
                           <tr>
                                <td colspan="2"><table width="100%" border="0"  height="113" class="container">
                                    <tr>
                                      <td rowspan="3" width="85">
											<a href='<?php echo $link; ?>'>
												<img src='<?php echo $productThumb; ?>' border='0'>											</a>										</td>
                                      <td  width="10" height="43" style="font-size:14px">&nbsp;</td>
                                      <td  width="284" style="font-size:14px"><a href='<?php echo $link; ?>'><?php echo $productName; ?></a></td>
                                      <td width="110">Wishing price</td>
                                      <td colspan="2">Offer</td>
                                      </tr>
                                      <tr>
                                        <td>&nbsp;</td>
                                      <td>&nbsp;&nbsp;&nbsp;Location : <?php echo $userCity ?></td>
                                      <td><?php echo $productPrice ?></td>
                                      <td colspan="2" rowspan="2" valign="top">

<form id="form1" name="frm<?php echo $itemId;?>" method="post" action="">
                                        <table width="100%" border="0">
                                          <tr>
                                            <td>Offer Price </td>
                                            <td><input type="text" name="txtOffer" /></td>
                                          </tr>
                                          <tr>
                                            <td>Comment</td>
                                            <td><textarea name="txtComment" cols="20"></textarea></td>
                                          </tr>
                                          <tr>
                                            <td>&nbsp;</td>
                                            <td><input type="submit" name="Submit" value="Make Offer" onclick="return sendOffer('<?php echo $itemId;?>')"/><input type="hidden" name="recieverid" value="<?php echo $rowSelWish['user_id'];?>"/>
                                <input type="hidden" name="pname" value="<?php echo $productName;?>"/>
								<input type="hidden" name="prodId" value="<?php echo $itemId;?>"/>
								<input type="hidden" name="section" value="WSS"/>
                                <input type="hidden" name="hidBargain" value="1"/></td>
                                          </tr>
                                        </table>
                                                                            </form></td>
                                      </tr>
                                      <tr>
                                        <td>&nbsp;</td>
                                      <td><strong><?php echo $userName;?></strong></td>
                                      <td></td>
                                      </tr>
                                      </table>
                             </td></tr>
							   <tr>
									 <td colspan="2">&nbsp;</td>
							   </tr>				 	
						 <?php } ?>				 
						</table>
				    <?php }
					} ?>
                                    <tr>
                                      <td><div align="center">
                                        <?php
				   $link=$pLink;
				   echo $disp=getPagingLink($sqlSelWish,$itemPerPage,$link);							   
			 ?>
                                      </div></td>
                                    </tr>
                                  </table>
                                </div>
			</td>
  		</tr>
                                </table>
	
	</td>
  </tr>
</table>
</div>